package com.ewing.busi.sysparam.dto;

import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;

import com.ewing.busi.cache.CacheConstants;
import com.ewing.busi.cache.CacheLoaders;
import com.ewing.busi.sysparam.model.SysParam;
import com.ewing.core.cache.CacheManager;
import com.google.common.collect.Lists;

public class SysParamDto {

  public SysParamDto(String rootCode) {
    this.rootCode = rootCode;
  }

  private SysParamDto(String name, String code, String value) {
    super();
    this.rootCode = null;
    this.name = name;
    this.value = value;
    this.code = code;
  }

  private String rootCode;

  private String name;

  private String value;

  private String code;

  public String getName() {
    return name;
  }

  public String getValue() {
    return value;
  }

  public String getCode() {
    return code;
  }

  public List<SysParamDto> getAll() {
    List<SysParamDto> result = Lists.newArrayList();

    List<SysParam> list = null;
    try {
      list = CacheManager.getInstance().get(CacheConstants.SYSTEMPARAM, this.rootCode,
          CacheLoaders.getInstance().getSysParamCacheLoader());
    } catch (Exception e) {
      e.printStackTrace();
      return Collections.emptyList();
    }
    for (SysParam param : list) {
      result
          .add(new SysParamDto(param.getParamName(), param.getParamCode(), param.getParamValue()));
    }
    return result;
  }

  public SysParamDto parse(String value) {
    if (StringUtils.isEmpty(value)) {
      return null;
    }

    List<SysParam> list = null;
    try {
      list = CacheManager.getInstance().get(CacheConstants.SYSTEMPARAM, this.rootCode,
          CacheLoaders.getInstance().getSysParamCacheLoader());
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
    for (SysParam param : list) {
      if (ObjectUtils.equals(param.getParamValue(), value)) {
        return new SysParamDto(param.getParamName(), param.getParamCode(), param.getParamValue());
      }
    }

    return null;
  }

  public String getName(String value) {
    if (StringUtils.isEmpty(value)) {
      return StringUtils.EMPTY;
    }

    SysParamDto way = parse(value);
    return null != way ? way.getName() : StringUtils.EMPTY;
  }

  public String getCode(String value) {
    if (StringUtils.isEmpty(value)) {
      return StringUtils.EMPTY;
    }

    SysParamDto way = parse(value);
    return null != way ? way.getCode() : StringUtils.EMPTY;
  }

  public String getNameByCode(String code) {
    if (StringUtils.isEmpty(code)) {
      return StringUtils.EMPTY;
    }

    List<SysParam> list = null;
    try {
      list = CacheManager.getInstance().get(CacheConstants.SYSTEMPARAM, this.rootCode,
          CacheLoaders.getInstance().getSysParamCacheLoader());
    } catch (Exception e) {
      e.printStackTrace();
      return StringUtils.EMPTY;
    }
    for (SysParam param : list) {
      if (ObjectUtils.equals(param.getParamCode(), code)) {
        return param.getParamName();
      }
    }

    return StringUtils.EMPTY;
  }

}
